﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using MiWebSite_Core;
using WLMCoreLibrary;

namespace MWS_Main
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
            List<ParametroTransformacionXML> parametros = new List<ParametroTransformacionXML>();
            parametros.Add( new ParametroTransformacionXML( "isHome", "true" ) );
            parametros.Add( new ParametroTransformacionXML( "isMobile", MiWebSiteFunctions.isMobileBrowser().ToString().ToLower() ) );
            parametros.Add( new ParametroTransformacionXML( "skinPath", config.Settings["skinPath"].value ) );
            if ( Request.QueryString["xml"] != null )
            {
                if ( Application["portada"] == null || HttpContext.Current.IsDebuggingEnabled )
                {
                    dbConnector ConexionDB = new dbConnector( config.Connections[0].value );
                    MiWebSitePage pagina = new MiWebSitePage( ref ConexionDB );
                    MiWebSiteFunctions.LoadIncludesWebSite( pagina );
                    if ( CurrentUser.IdUsuario != 0 )
                        MiWebSiteFunctions.LoadIncludesWebSiteAdmin( pagina );
                    XmlDocument xmlModulos = new XmlDocument();
                    if ( config.Settings["XMLPortada"].value.Contains( "http://" ) )
                        xmlModulos.Load( config.Settings["XMLPortada"].value );
                    else
                        xmlModulos.Load( config.Settings["siteURL"].value + config.Settings["XMLPortada"].value );
                    XPathNavigator navigator = xmlModulos.CreateNavigator();
                    XPathExpression selectExpression = navigator.Compile( "WebSiteModules/module" );
                    XPathExpression sortExpr = navigator.Compile( "position" );
                    selectExpression.AddSort( sortExpr, new OrderComparer() );
                    XPathNodeIterator ListaModulosXML = navigator.Select( selectExpression );
                    pagina.Contenido = xmlModulos.CreateElement( "Portada" );
                    while ( ListaModulosXML.MoveNext() )
                    {
                        XmlDocument XMLModuloActual = new XmlDocument();
                        XMLModuloActual.Load( ListaModulosXML.Current.SelectSingleNode( "DataURL" ).Value );

                        XmlNode XMLcontenidoModulo = XMLModuloActual.DocumentElement;
                        XmlNode XMLModuloExtraido = xmlModulos.ImportNode( XMLcontenidoModulo, true );
                        pagina.Contenido.AppendChild( XMLModuloExtraido );
                    }
                    string xmlportada = WLMFunctions.SerializeObject( pagina, true );
                    ConexionDB.CloseConnection();
                    Application["portada"] = xmlportada;
                }
                Response.ContentType = "text/xml";
                Response.Write( Application["portada"] );
            }
            else
            {
                string HTMLPagina = WLMFunctions.TransformXML( config.Settings["siteURL"].value + "?xml=true", config.Settings["siteURL"].value + config.Settings["skinPath"].value + "index.xsl", parametros );
                Response.Write( HTMLPagina );
            }
        }
    }
}